package com.yq.skinlib.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.yq.skinlib.manage.SkinCompatManager;


/**
 * Created by ximsfei on 2017/1/10.
 */

public class SkinPreference {
    private static final String FILE_NAME = "meta-data";

    private static final String KEY_SKIN_NAME = "skin-name";
    private static final String KEY_SKIN_STRATEGY = "skin-strategy";
    private static final String KEY_SKIN_ID = "skin-id";
    private static final String KEY_SKIN_IS_DEFAULT = "skin-is-default";
    private static final String KEY_SKIN_USER_THEME = "skin-user-theme-json";
    private static final String KEY_SKIN_NIGHT_MODE = "skin-night-mode";
    private static final String KEY_SKIN_USE_FILTER = "skin-use-filter";
    private static final String KEY_SKIN_VERSION = "skin-version";
    private static final String KEY_TEXT_SIZE = "text_size";

    private static final String KEY_LAST_SKIN_NAME = "last-skin-name";
    private static final String KEY_LAST_SKIN_ID = "last-skin-id";
    private static final String KEY_LAST_SKIN_ISDEFAULT = "last-skin-isdefault";
    private static final String KEY_LAST_SKIN_STRATEGY = "last-skin-stragegy";

    private static SkinPreference sInstance;
    private final Context mApp;
    private final SharedPreferences mPref;
    private final SharedPreferences.Editor mEditor;

    public static void init(Context context) {
        if (sInstance == null) {
            synchronized (SkinPreference.class) {
                if (sInstance == null) {
                    sInstance = new SkinPreference(context.getApplicationContext());
                }
            }
        }
    }

    public static SkinPreference getInstance() {
        return sInstance;
    }

    private SkinPreference(Context applicationContext) {
        mApp = applicationContext;
        mPref = mApp.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        mEditor = mPref.edit();
    }

    public SkinPreference setSkinName(String skinName) {
        mEditor.putString(KEY_SKIN_NAME, skinName);
        return this;
    }

    public String getSkinName() {
        return mPref.getString(KEY_SKIN_NAME, "");
    }

    public SkinPreference setSkinId(String skinId) {
        mEditor.putString(KEY_SKIN_ID, skinId);
        return this;
    }

    public String getSkinId() {
        return mPref.getString(KEY_SKIN_ID, "");
    }

    public SkinPreference setSkinStrategy(int strategy) {
        mEditor.putInt(KEY_SKIN_STRATEGY, strategy);
        return this;
    }

    public int getSkinStrategy() {
        return mPref.getInt(KEY_SKIN_STRATEGY, SkinCompatManager.SKIN_LOADER_STRATEGY_NONE);
    }


    public SkinPreference setUseFilter(boolean useFilter) {
        mEditor.putBoolean(KEY_SKIN_USE_FILTER, useFilter);
        return this;
    }

    public boolean getUseFilter() {
        return mPref.getBoolean(KEY_SKIN_USE_FILTER, false);
    }

    public SkinPreference setNightMode(boolean isNightMode) {
        mEditor.putBoolean(KEY_SKIN_NIGHT_MODE, isNightMode);
        return this;
    }

    public int getSkinVersion() {
        return mPref.getInt(KEY_SKIN_VERSION, 1);
    }

    public SkinPreference setSkinVersion(int skinVersion) {
        mEditor.putInt(KEY_SKIN_VERSION, skinVersion);
        return this;
    }

    public boolean getIsNightMode() {
        return mPref.getBoolean(KEY_SKIN_NIGHT_MODE, false);
    }


    public SkinPreference setUserTheme(String themeJson) {
        mEditor.putString(KEY_SKIN_USER_THEME, themeJson);
        return this;
    }

    public String getUserTheme() {
        return mPref.getString(KEY_SKIN_USER_THEME, "");
    }

    public float getTextSize() {
        return mPref.getFloat(KEY_TEXT_SIZE, 0.9f);
    }

    public SkinPreference setTextSize(float skinVersion) {
        mEditor.putFloat(KEY_TEXT_SIZE, skinVersion);
        return this;
    }

    public SkinPreference setLastSkinName(String lastSkinName) {
        mEditor.putString(KEY_LAST_SKIN_NAME, lastSkinName);
        return this;
    }

    public String getLastSkinName() {
        return mPref.getString(KEY_LAST_SKIN_NAME,"");
    }

    public SkinPreference setLastSkinId(String lastSkinId) {
        mEditor.putString(KEY_LAST_SKIN_ID, lastSkinId);
        return this;
    }

    public String getLastSkinId() {
        return mPref.getString(KEY_LAST_SKIN_ID,"");
    }

    public SkinPreference setLastSkinIsDefault(boolean isDefault) {
        mEditor.putBoolean(KEY_LAST_SKIN_ISDEFAULT, isDefault);
        return this;
    }

    public boolean getLastSkinIsDefault() {
        return mPref.getBoolean(KEY_LAST_SKIN_ISDEFAULT,false);
    }

    public SkinPreference setLastSkinStrategy(int skinStrategy) {
        mEditor.putInt(KEY_LAST_SKIN_STRATEGY, skinStrategy);
        return this;
    }

    public int getLastSkinStrategy() {
        return mPref.getInt(KEY_LAST_SKIN_STRATEGY,1);
    }



    public void commitEditor() {
        mEditor.apply();
    }
}
